package com.bdc.whatwhat.task;

import android.os.AsyncTask;

import com.bdc.whatwhat.Service.Service;
import com.bdc.whatwhat.modele.MonFlux;
import com.bdc.whatwhat.modele.WhatWhat;

import java.util.List;

/**
 * Created by florentchampigny on 25/06/14.
 */
public class MonFluxTask extends AsyncTask<Object, Void,MonFlux> {

    public interface FluxTaskDelegate {
        public void afficherChargement(boolean afficher);

        public void onFluxRecu(MonFlux w);

        public void onErreurReseau();
    }

    FluxTaskDelegate delegate;
    boolean erreurReseau;


    public MonFluxTask(FluxTaskDelegate delegate) {
        this.delegate = delegate;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (delegate != null)
            delegate.afficherChargement(true);
    }

    @Override
    protected MonFlux doInBackground(Object... objects) {

        MonFlux whatWhats = null;
        String token = (String) objects[0];

        try {
            whatWhats = Service.monFlux(token);
        } catch (Service.NetworkException e) {
            erreurReseau = true;
            e.printStackTrace();
        }

        return whatWhats;
    }

    @Override
    protected void onPostExecute(MonFlux whatWhats) {
        super.onPostExecute(whatWhats);
        if (delegate != null) {
            delegate.afficherChargement(false);
            if (erreurReseau)
                delegate.onErreurReseau();
            else
                delegate.onFluxRecu(whatWhats);
        }
    }
}
